package views;

import java.util.ArrayList;
import java.util.List;

import utils.MessageUtils;
import utils.NavigatorUtils;
import utils.SessionUtils;
import views.components.AppLink;
import views.components.Header;
import views.interfaces.INovice;
import app.security.ResSecLevel;

import com.vaadin.annotations.Theme;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.navigator.View;
import com.vaadin.navigator.ViewChangeListener.ViewChangeEvent;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.CheckBox;
import com.vaadin.ui.Alignment;
//import com.vaadin.ui.CssLayout;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Notification;
//import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;

import config.Parameters;

@SuppressWarnings("serial")
@Theme("Theme02")
/**
 * Clase que es mostrara despres de fer login, que mostra les aplicacions disponibles per al usuari
 *
 */
public class MainView extends CustomComponent implements View, INovice {

	private HorizontalLayout contenedorColumnes, contenedorSuperior;
	private VerticalLayout root, col1, col2, col3;

	private List<AppLink> appLinks;
	private Button btlogout;
	private CheckBox checkBoxNovice;
	boolean novice;
	private Header header;

	public MainView() {

		iniciarVariables();

		// Add applications from profile

		int n_app = 0;

		AppLink appLink;
		List<ResSecLevel> applications = SessionUtils.getSessionApplications();

		for (ResSecLevel rs : applications) {

			appLink = new AppLink(rs.getIcon(), MessageUtils.getI18n(rs
					.getName()), MessageUtils.getI18nAppLong(rs.getName())
					+ ".\n", rs.getName(), Parameters.APP_VIEW_NAME, novice);

			appLinks.add(appLink);

			if (n_app % 3 == 0)
				col1.addComponent(appLink);
			else if (n_app % 3 == 1)
				col2.addComponent(appLink);
			else if (n_app % 3 == 2)
				col3.addComponent(appLink);

			++n_app;
		}

	}

	private void iniciarVariables() {
		novice = false;
		checkBoxNovice = new CheckBox(MessageUtils.getI18n("novice"), novice);

		appLinks = new ArrayList<AppLink>();
		header = new Header(MessageUtils.getI18n("menu"));
		checkBoxNovice.addStyleName("HEADER_TITLE3");

		checkBoxNovice.addValueChangeListener(new ValueChangeListener() {
			@Override
			public void valueChange(ValueChangeEvent event) {
				boolean noviceCheckBox = (Boolean) event.getProperty()
						.getValue();
				SessionUtils.setSessionNovice(noviceCheckBox);
				setNovice(noviceCheckBox);

			}
		});
		btlogout = new Button(MessageUtils.getI18n("logout"),
				new Button.ClickListener() {
					@Override
					public void buttonClick(ClickEvent event) {

						// "Logout" the user
						SessionUtils.setSessionUser(null);
						SessionUtils.setSessionAttribute("AppList", null);
						try {
							NavigatorUtils.navigateTo(getUI().getNavigator(),
									Parameters.LOGOUT_VIEW_NAME);
						} catch (Exception e) {
							Notification.show(e.getMessage());
						}

					}
				});

		col1 = new VerticalLayout();
		col2 = new VerticalLayout();
		col3 = new VerticalLayout();
		contenedorColumnes = new HorizontalLayout(col1, col2, col3);
		contenedorSuperior = new HorizontalLayout(checkBoxNovice, btlogout);
		root = new VerticalLayout(header, contenedorSuperior,
				contenedorColumnes);
		root.setComponentAlignment(contenedorSuperior, Alignment.TOP_RIGHT);
		root.setComponentAlignment(contenedorColumnes, Alignment.TOP_CENTER);
		setCompositionRoot(root);

		col1.setMargin(true);
		col1.setSpacing(true);
		col2.setMargin(true);
		col2.setSpacing(true);
		col3.setMargin(true);
		col3.setSpacing(true);
		root.setMargin(true);
		root.setSpacing(true);
		contenedorColumnes.setMargin(true);
		contenedorColumnes.setSpacing(true);

		contenedorSuperior.setMargin(true);
		contenedorSuperior.setSpacing(true);
	}

	@Override
	public void enter(ViewChangeEvent event) {

	}

	@Override
	public void setNovice(boolean novice) {
		for (AppLink appLink : appLinks)
			appLink.setNovice(novice);
	}
}
